<?php
namespace system\core;

use system\core\Request;

/**
 * Config
 * 
 * This class does everything related to the config files in: application/config
 * 
 * @author Steffen Brem <steffenbrem@gmail.com>
 * @package System.core
 */
class Config 
{
    /**
     * Stores all the config items
     * 
     * @var array
     */
    private static $_config = array();
    
    public static function set( $file, $option, $value )
    {
        if ( isset(self::$_config[$file][$option]) )
        {
            self::$_config[$file][$option] = $value;
        }
        else
        {
            echo 'Not isset<br />';
        }
    }
    
    /**
     * Get a config item or item's
     * 
     * The first parameter has to be the exact filename of the config file. 
     * 
     * If you dont specify the second parameter, it will return all the items
     * in config file as an array.
     * 
     * @param string $file
     * @param string $option
     * @return mixed
     */
    public static function get( $section, $option = NULL, $overwrite = FALSE )
    {
        if ( isset(self::$_config[$section]) )
        {

            if ( $option )
            {
                if ( ! key_exists($option, self::$_config[$section]) )
                {
                    throw new \Exception('The setting "' . $option . '" does not 
                        exist in section "' . $section . '". Check your settings.ini file.');
                }
                
                return self::$_config[$section][$option];
            }
            
            return self::$_config[$section];
        }
        else
        {
            $filePath = 'config/settings.ini';
            
            $request = Request::getInstance();
            if ( $request instanceof Request && $request->getModule() )
            {
                $allowed = array('template', 'database');
                if ( in_array($section, $allowed) )
                {
                    if ( $section == 'template' || file_exists(APP_PATH . 'modules/' . $request->getModule() . '/config/settings.ini') )
                    {
                        $filePath = 'modules/' . $request->getModule() . '/config/settings.ini';
                    }
                }
            }
            
            if ( file_exists(APP_PATH . $filePath) )
            {
                $ini = self::parseIni(APP_PATH . $filePath);
                
                // Save the settings in the config property
                foreach ( $ini as $_section => $settings )
                {
                    self::$_config[$_section] = $settings;
                }
                
                if ( $option )
                {
                    if ( ! key_exists($option, self::$_config[$section]) )
                    {
                        throw new \Exception('The setting "' . $option . '" does not 
                        exist in section "' . $section . '". Check your settings.ini file.');
                    }

                    return self::$_config[$section][$option];
                }

                return self::$_config[$section];

            }
            else
            {
                throw new \Exception('Settings file "' . $filePath . '" does not exist.');
            }
        }
    }
    
    /**
     * Parses the ini file and returns it as an well formed array
     * 
     * @return array
     * @access private
     */
    public static function parseIni( $path )
    {
        $ini = parse_ini_file($path, TRUE);
        foreach ( $ini as $section => $settings )
        {
            foreach ( $settings as $setting => $value )
            {
                $groups = explode('.', $setting);
                if ( count($groups) > 1 )
                {
                    $ini[$section][$groups[0]][$groups[1]] = $value;
                    unset($ini[$section][$setting]);
                }
               
            }
        }
        
        return $ini;
    }
}

/* End of file Config.php */
